<?php
/**
 * File : FormManager.php
 * Created by Ludovic Reenaers
 * Created on  12 nov. 2010
 * lreenaers@hotmail.com
 */
//TODO: use js to auto complete form fields or error
class FormManager{
	private $token;
	private $errors;
	private $errBool;
	private $formValues;//array[field] = value;
	function __construct(){
		$this->token =  null;
		$this->errors = array();
		$this->errBool = false;
		$this->formValues = null;
		
	}
	function start(){
		
		
		if(is_null($this->token) || $this->token <> $_SESSION['module']."#".$_SESSION['action']){
			$this->token = $_SESSION['module']."#".$_SESSION['action'];
			$this->errors = array();
			$this->errBool = false;
			$this->formValues = null;
			
		}
	}
	function stop(){
		$this->token = null;
		$this->errors = array();
		$this->errBool = false;
		$this->formValues = null;
	}
	function dumpErrors(){ 
		$str = "";
		foreach ($this->errors as $excep){
			$str.='<li>'.$excep->getMessage().'</li>';
		}
		
		return $str;
	}
	function addError($Exception){
		if(is_a($Exception,'Exception')){
			$this->errBool = true;
			array_push($this->errors,$Exception);
		}
	}
	function hasError(){
		return $this->errBool;
	}
	function cleanErrors(){
		$this->errors = array();
		$this->errBool = false;
	}
	function addFormContent($fieldId, $fieldValue){
		$this->formValues[$fieldId] = $fieldValue;
	}
}
?>